'use strict';


const Service = require('egg').Service;

const md5 = require('md5');
const ulid = require('../public/ulid.js');
const path = require('path');
const fs = require('fs');
class TenantAdminService extends Service {
  async tenantAdminList(params) {
    const Op = this.ctx.model.Sequelize.Op;
    if (params.search && params.search != '') {
      const result = await this.ctx.model.TenantAdmin.findAndCountAll({
        offset: parseInt((params.page - 1) * params.limit),
        limit: parseInt(params.limit),
        where: {
          name: { [Op.like]: `%${params.search}%` },
        },
      });
      return result;
    }
    const result = await this.ctx.model.TenantAdmin.findAndCountAll({
      offset: parseInt((params.page - 1) * params.limit),
      limit: parseInt(params.limit),
    });
    return result;

  }
  async tenantAdminCreate(params) {
    const tid = ulid.ulid();
    const uid = ulid.ulid();
    const password = md5(params.mpassword);
    await this.ctx.model.TenantAdmin.create({
      id: tid,
      initFlag: params.initFlag,
      memo: params.memo,
      mpassword: password,
      muser: params.muser,
      name: params.name,
      enabled: params.enabled,
      state: false,
    });
    // TODO 这里需要改
    await this.ctx.model.UserAdmin.create({
      tenantadminId: tid,
      username: params.muser,
      password,
      nickname: params.name,
      is_super: true,
      state: true,
      is_company: true,
      companyname: params.name,
      language: 'en',
      outsidestaff: false,
      id: uid,
    });
  }
  async tenantAdminSingle(username) {
    const result = await this.ctx.model.TenantAdmin.findOne({
      where: { name: username },
    });
    return result;
  }
  async tenantAdminEdit(params) {
    await this.ctx.model.TenantAdmin.update({
      enabled: params.enabled,
      memo: params.memo,
      name: params.name,
    },
    {
      where: {
        id: params.id,
      },
    });
  }
  async acsInit(params) {
    await this.ctx.model.TenantAdmin.update({
      acsURL: params.url,
    },
    {
      where: {
        id: params.id,
      },
    });
  }
  async getACS(params) {
    console.log(this.ctx.cookies.get('token'));
    const result = await this.ctx.model.TenantAdmin.findOne({
      where: {
        id: params.tid,
      },
    });
    return result;
  }
  async tenantAdminDelete(params) {
    await this.ctx.model.TenantAdmin.destroy({ where: { id: params.id } });
  }
  async tenantAdminInit(params, userid) {
    try {
      // await InitSpeedTemplate(params.id, this);
      await InitOltTypeAndScript(params.id, this);
      // await InitConfig(params.id, this);
      let result = await this.ctx.modelmysql.RoleInfo.findOne({where:{tid:params.id,name:"super"}})
      if(result == null){
        await createSuper(params.id, this);
        await createUserRole(params.id, this, userid);
      }
      // await this.ctx.model.TenantAdmin.update({ initFlag: true }, { where: { id: params.id } });
      return true;
    } catch (error) {
      return false;
    }
  }
  async tenantAdmintr069Init(params) {
    try {
      await InitTr069Command(params.id, this);
      return true;
    } catch (error) {
      return false;
    }
  }
  async tenantAdminListAll() {
    const result = await this.ctx.model.TenantAdmin.findAndCountAll();
    return result;
  }
  async editPassword(params) {
    const password = md5(params.password);
    // TODO 这里需要改
    await this.ctx.model.UserAdmin.update({
      password,
    }, {
      where: {
        tenantadminid: params.id,
        username: 'admin',
      },
    });
  }
  async single(tid) {
    return await this.ctx.model.TenantAdmin.findOne({
      where: { id: tid },
    });
  }
  async tenantftpedit(params) {
    const tid = params.tid;
    await this.ctx.model.TenantAdmin.update({
      ftpIp: params.ftpIp,
      ftpPassword: params.ftpPassword,
    }, {
      where: {
        id: tid,
      },
    });
  }
  async cloudOltServerInit(params) {
    const result = await this.ctx.modelmysql.TenantAdmin.update({
      cloudOltURL: params.url,
      cloudOltState:params.state
    },
    {
      where: {
        id: params.id,
      },
    });
    return {
      msg:'success'
    }
  }
}
// async function InitSpeedTemplate(tid, me) {
//   const p = [
//     2,
//     4,
//     8,
//     10,
//     20,
//     30,
//     40,
//     50,
//     60,
//     70,
//     80,
//     90,
//     100,
//     200,
//     300,
//     400,
//     500,
//     1000,
//   ];
//   const speedtemplateArr = p.map(item => {
//     return {
//       id: ulid.ulid(),
//       name: `${item}M`,
//       Speed: item,
//       tid,
//     };
//   });
//   await me.ctx.model.SpeedTemplate.destroy({ where: { tid } });
//   await me.ctx.model.SpeedTemplate.bulkCreate(speedtemplateArr);
// }
async function InitOltTypeAndScript(tid, me) {
  await me.ctx.modelmysql.OltType.destroy({ where: { tid } });
  await me.ctx.modelmysql.Script.destroy({ where: { tid } });
  const data = await readFile(path.join(__dirname, '../data/scriptJSON.json'));
  const scriptObj = JSON.parse(data);
  for (let j = 0; j < scriptObj.length; j++) {
    const item = scriptObj[j];
    try {
      const obj = {
        id: ulid.ulid(),
        name: item.oltTypeName,
        tid,
      };
      const obj_olt = await me.ctx.modelmysql.OltType.create(obj);
      if (item.scriptArr.length > 0) {
        for (let i = 0; i < item.scriptArr.length; i++) {
          const scriptItem = item.scriptArr[i];
          try {
            const info = await readFile(
              path.join(__dirname, scriptItem.scriptSrc)
            );
            const scriptInfo = {
              id: ulid.ulid(),
              name: scriptItem.scriptName,
              script: info.replace(/\n/g, '<br>'),
              MountPoint: scriptItem.MountPoint,
              version: scriptItem.version,
              olttypeId: obj_olt.id,
              tid,
            };
            await me.ctx.modelmysql.Script.create(scriptInfo);
          } catch (error) {
            console.log(error);
          }
        }
      }
    } catch (error) {
      console.log(error);
    }
  }
}

// async function InitConfig(tid, me) {
//   await me.ctx.model.Config.destroy({ where: { tid } });
//   await me.ctx.model.Config.create({
//     id: ulid.ulid(),
//     flag: false,
//     refreshtime: 5,
//     days: 5,
//     tid,
//   });
// }

async function createSuper(tid, me) {
  const roleCode = [];
  const result = await me.ctx.modelmysql.Menu.findAll();
  for (let i = 0; i < result.length; i++) {
    roleCode.push('"' + result[i].id + '"');
  }
  await me.ctx.modelmysql.RoleInfo.destroy({ where: { tid } });
  await me.ctx.modelmysql.RoleInfo.create({
    id: ulid.ulid(),
    name: 'super',
    menus: '[' + roleCode + ']',
    RoleCode: '1',
    tid,
  });
}

async function createUserRole(tid, me, userid) {
  await me.ctx.modelmysql.UserRole.destroy({ where: { tid } });
  // const userResult = await me.ctx.model.UserAdmin.findOne({ where: { username: 'admin', tenantadminId: tid } });
  const roleResult = await me.ctx.modelmysql.RoleInfo.findOne({ where: { name: 'super', tid } });
  await me.ctx.modelmysql.UserRole.create({
    id: ulid.ulid(),
    tid,
    userid,
    roleid: roleResult.id,
  });
}

async function readFile(dirname) {
  return new Promise((resolve, reject) => {
    fs.readFile(dirname, function(err, data) {
      if (err) {
        reject(err);
      } else {
        resolve(data.toString()); // 输出utf8 格式
      }
    });
  });
}
async function InitTr069Command(tid, me) {
  await me.ctx.modelmysql.Tr069Command.destroy({ where: { tid } });
  const wanfixip = [];
  wanfixip.push({
    key: 'InternetGatewayDevice.WANDevice.1.WANConnectionDevice.$.WANIPConnection.$.X_RTK_VlanId',
    name: 'vlanNumber',
  });
  wanfixip.push({
    key: 'InternetGatewayDevice.WANDevice.1.WANConnectionDevice.$.WANIPConnection.$.AddressingType',
    name: 'AddressingType',
  });
  wanfixip.push({
    key: 'InternetGatewayDevice.WANDevice.1.WANConnectionDevice.$.WANIPConnection.$.ConnectionType',
    name: 'ConnectionType',
  });
  // addressingtype=static时 不配置DnsEnabled
  // wanfixip.push({
  //   key: 'InternetGatewayDevice.WANDevice.1.WANConnectionDevice.$.WANIPConnection.$.DNSEnabled',
  //   name: 'DNSEnabled',
  // });
  wanfixip.push({
    key: 'InternetGatewayDevice.WANDevice.1.WANConnectionDevice.$.WANIPConnection.$.DNSServers',
    name: 'dns',
  });
  wanfixip.push({
    key: 'InternetGatewayDevice.WANDevice.1.WANConnectionDevice.$.WANIPConnection.$.ExternalIPAddress',
    name: 'ip',
  });
  wanfixip.push({
    key: 'InternetGatewayDevice.WANDevice.1.WANConnectionDevice.$.WANIPConnection.$.SubnetMask',
    name: 'wanSubnetMask',
  });
  wanfixip.push({
    key: 'InternetGatewayDevice.WANDevice.1.WANConnectionDevice.$.WANIPConnection.$.DefaultGateway',
    name: 'wanGatewayIP',
  });
  wanfixip.push({
    key: 'InternetGatewayDevice.WANDevice.1.WANConnectionDevice.$.WANIPConnection.$.X_RTK_LanInterface',
    name: 'bindPortList',
  });
  wanfixip.push({
    key: 'InternetGatewayDevice.WANDevice.1.WANConnectionDevice.$.WANIPConnection.$.NATEnabled',
    name: 'NATEnabled',
  });
  wanfixip.push({
    key: 'InternetGatewayDevice.WANDevice.1.WANConnectionDevice.$.WANIPConnection.$.X_RTK_ServiceType',
    name: 'businesstype',
  });
  wanfixip.push({
    key: 'InternetGatewayDevice.WANDevice.1.WANConnectionDevice.$.WANIPConnection.$.Enable',
    name: 'Enable',
  });
  const lan = [];

  lan.push({
    key: 'InternetGatewayDevice.LANDevice.1.LANHostConfigManagement.IPInterface.1.IPInterfaceIPAddress',
    name: 'ip',
  });
  lan.push({
    key: 'InternetGatewayDevice.LANDevice.1.LANHostConfigManagement.IPRouters',
    name: 'ip',
  });
  lan.push({
    key: 'InternetGatewayDevice.LANDevice.1.LANHostConfigManagement.DHCPServerEnable',
    name: 'dhcp',
  });
  lan.push({
    key: 'InternetGatewayDevice.LANDevice.1.LANHostConfigManagement.DNSServers',
    name: 'dhcpdns',
  });
  lan.push({
    key: 'InternetGatewayDevice.LANDevice.1.LANHostConfigManagement.MaxAddress',
    name: 'maxAddress',
  });
  lan.push({
    key: 'InternetGatewayDevice.LANDevice.1.LANHostConfigManagement.MinAddress',
    name: 'minAddress',
  });
  const bridgearr = [];
  bridgearr.push({
    key: 'InternetGatewayDevice.WANDevice.1.WANConnectionDevice.$.WANIPConnection.$.ConnectionType',
    name: 'bridgetype',
  });
  bridgearr.push({
    key: 'InternetGatewayDevice.WANDevice.1.WANConnectionDevice.$.WANIPConnection.$.X_RTK_ServiceType',
    name: 'businesstype',
  });
  bridgearr.push({
    key: 'InternetGatewayDevice.WANDevice.1.WANConnectionDevice.$.WANIPConnection.$.X_RTK_VlanId',
    name: 'vlanNumber',
  });
  bridgearr.push({
    key: 'InternetGatewayDevice.WANDevice.1.WANConnectionDevice.$.WANIPConnection.$.X_RTK_LanInterface',
    name: 'bindPortList',
  });
  bridgearr.push({
    key: 'InternetGatewayDevice.WANDevice.1.WANConnectionDevice.$.WANIPConnection.$.ConnectionType',
    name: 'ConnectionType',
  });
  bridgearr.push({
    key: 'InternetGatewayDevice.WANDevice.1.WANConnectionDevice.$.WANIPConnection.$.Enable',
    name: 'Enable',
  });
  bridgearr.push({
    key: 'InternetGatewayDevice.WANDevice.1.WANConnectionDevice.$.WANPPPConnection.$.ConnectionType',
    name: 'bridgetype',
  });
  bridgearr.push({
    key: 'InternetGatewayDevice.WANDevice.1.WANConnectionDevice.$.WANPPPConnection.$.X_RTK_ServiceType',
    name: 'businesstype',
  });
  bridgearr.push({
    key: 'InternetGatewayDevice.WANDevice.1.WANConnectionDevice.$.WANPPPConnection.$.X_RTK_VlanId',
    name: 'vlanNumber',
  });
  bridgearr.push({
    key: 'InternetGatewayDevice.WANDevice.1.WANConnectionDevice.$.WANPPPConnection.$.X_RTK_LanInterface',
    name: 'bindPortList',
  });
  bridgearr.push({
    key: 'InternetGatewayDevice.WANDevice.1.WANConnectionDevice.$.WANPPPConnection.$.ConnectionType',
    name: 'ConnectionType',
  });
  bridgearr.push({
    key: 'InternetGatewayDevice.WANDevice.1.WANConnectionDevice.$.WANPPPConnection.$.Enable',
    name: 'Enable',
  });
  const siparr = [];
  siparr.push({
    key: 'InternetGatewayDevice.Services.VoiceService.1.VoiceProfile.1.SIP.ProxyServer',
    name: 'sip1ServerAddress',
  });
  siparr.push({
    key: 'InternetGatewayDevice.Services.VoiceService.1.VoiceProfile.1.SIP.ProxyServerPort',
    name: 'sip1ServerPort',
  });
  siparr.push({
    key: 'InternetGatewayDevice.Services.VoiceService.1.VoiceProfile.1.SIP.OutboundProxy',
    name: 'outboundServerAddress',
  });
  siparr.push({
    key: 'InternetGatewayDevice.Services.VoiceService.1.VoiceProfile.1.SIP.OutboundProxyPort',
    name: 'outboundServerPort',
  });
  siparr.push({
    key: 'InternetGatewayDevice.Services.VoiceService.1.VoiceProfile.1.SIP.RegistrarServer',
    name: 'domain',
  });
  siparr.push({
    key: 'InternetGatewayDevice.Services.VoiceService.1.VoiceProfile.1.Line.1.SIP.AuthUserName',
    name: 'userInfo',
  });
  siparr.push({
    key: 'InternetGatewayDevice.Services.VoiceService.1.VoiceProfile.1.Line.1.SIP.AuthPassword',
    name: 'password',
  });
  siparr.push({
    key: 'InternetGatewayDevice.Services.VoiceService.1.VoiceProfile.1.Line.1.Enable',
    name: 'enable',
  });
  siparr.push({
    key: 'InternetGatewayDevice.Services.VoiceService.1.VoiceProfile.1.Line.1.DirectoryNumber',
    name: 'directoryNumber',
  });

  const wanDHCPArr = [];
  wanDHCPArr.push({
    key: 'InternetGatewayDevice.WANDevice.1.WANConnectionDevice.$.WANIPConnection.$.X_RTK_ServiceType',
    name: 'businesstype',
  });
  wanDHCPArr.push({
    key: 'InternetGatewayDevice.WANDevice.1.WANConnectionDevice.$.WANIPConnection.$.X_RTK_VlanId',
    name: 'vlanNumber',
  });
  wanDHCPArr.push({
    key: 'InternetGatewayDevice.WANDevice.1.WANConnectionDevice.$.WANIPConnection.$.X_RTK_LanInterface',
    name: 'bindPortList',
  });
  wanDHCPArr.push({
    key: 'InternetGatewayDevice.WANDevice.1.WANConnectionDevice.$.WANIPConnection.$.AddressingType',
    name: 'AddressingType',
  });
  wanDHCPArr.push({
    key: 'InternetGatewayDevice.WANDevice.1.WANConnectionDevice.$.WANIPConnection.$.ConnectionType',
    name: 'ConnectionType',
  });
  wanDHCPArr.push({
    key: 'InternetGatewayDevice.WANDevice.1.WANConnectionDevice.$.WANIPConnection.$.NATEnabled',
    name: 'NATEnabled',
  });
  wanDHCPArr.push({
    key: 'InternetGatewayDevice.WANDevice.1.WANConnectionDevice.$.WANIPConnection.$.Enable',
    name: 'Enable',
  });
  const pppoeArr = [];
  pppoeArr.push({
    key: 'InternetGatewayDevice.WANDevice.1.WANConnectionDevice.$.WANPPPConnection.$.Username',
    name: 'pppoeusername',
  });
  pppoeArr.push({
    key: 'InternetGatewayDevice.WANDevice.1.WANConnectionDevice.$.WANPPPConnection.$.Password',
    name: 'pppoepassword',
  });
  pppoeArr.push({
    key: 'InternetGatewayDevice.WANDevice.1.WANConnectionDevice.$.WANPPPConnection.$.X_RTK_ServiceType',
    name: 'businesstype',
  });
  pppoeArr.push({
    key: 'InternetGatewayDevice.WANDevice.1.WANConnectionDevice.$.WANPPPConnection.$.X_RTK_VlanId',
    name: 'vlanNumber',
  });
  pppoeArr.push({
    key: 'InternetGatewayDevice.WANDevice.1.WANConnectionDevice.$.WANPPPConnection.$.X_RTK_LanInterface',
    name: 'bindPortList',
  });
  pppoeArr.push({
    key: 'InternetGatewayDevice.WANDevice.1.WANConnectionDevice.$.WANPPPConnection.$.NATEnabled',
    name: 'NATEnabled',
  });
  pppoeArr.push({
    key: 'InternetGatewayDevice.WANDevice.1.WANConnectionDevice.$.WANPPPConnection.$.ConnectionType',
    name: 'ConnectionType',
  });
  pppoeArr.push({
    key: 'InternetGatewayDevice.WANDevice.1.WANConnectionDevice.$.WANPPPConnection.$.Enable',
    name: 'Enable',
  });
  const wifi24Arr = [];
  wifi24Arr.push({
    key: 'InternetGatewayDevice.LANDevice.1.WLANConfiguration.6.Enable',
    name: 'wifistate',
  });
  wifi24Arr.push({
    key: 'InternetGatewayDevice.LANDevice.1.WLANConfiguration.6.SSID',
    name: 'ssidName',
  });
  wifi24Arr.push({
    key: 'InternetGatewayDevice.LANDevice.1.WLANConfiguration.6.X_RZ_BandWidth',
    name: 'channelwide',
  });
  wifi24Arr.push({
    key: 'InternetGatewayDevice.LANDevice.1.WLANConfiguration.6.Channel',
    name: 'channel',
  });
  wifi24Arr.push({
    key: 'InternetGatewayDevice.LANDevice.1.WLANConfiguration.6.BeaconType',
    name: 'encryption',
  });
  wifi24Arr.push({
    key: 'InternetGatewayDevice.LANDevice.1.WLANConfiguration.6.WPAAuthenticationMode',
    name: 'WPAAuthenticationMode',
  });
  wifi24Arr.push({
    key: 'InternetGatewayDevice.LANDevice.1.WLANConfiguration.6.WPAEncryptionModes',
    name: 'encryptionType',
  });
  wifi24Arr.push({
    key: 'InternetGatewayDevice.LANDevice.1.WLANConfiguration.6.PreSharedKey.1.KeyPassphrase',
    name: 'wpaSharedPassword',
  });
  wifi24Arr.push({
    key: 'InternetGatewayDevice.LANDevice.1.WLANConfiguration.6.AutoChannelEnable',
    name: 'AutoChannelEnable',
  });
  const wifi24Arr1 = [];
  wifi24Arr1.push({
    key: 'InternetGatewayDevice.LANDevice.1.WLANConfiguration.1.Enable',
    name: 'wifistate',
  });
  wifi24Arr1.push({
    key: 'InternetGatewayDevice.LANDevice.1.WLANConfiguration.1.SSID',
    name: 'ssidName',
  });
  wifi24Arr1.push({
    key: 'InternetGatewayDevice.LANDevice.1.WLANConfiguration.1.X_RZ_BandWidth',
    name: 'channelwide',
  });
  wifi24Arr1.push({
    key: 'InternetGatewayDevice.LANDevice.1.WLANConfiguration.1.Channel',
    name: 'channel',
  });
  wifi24Arr1.push({
    key: 'InternetGatewayDevice.LANDevice.1.WLANConfiguration.1.BeaconType',
    name: 'encryption',
  });
  wifi24Arr1.push({
    key: 'InternetGatewayDevice.LANDevice.1.WLANConfiguration.1.WPAAuthenticationMode',
    name: 'WPAAuthenticationMode',
  });
  wifi24Arr1.push({
    key: 'InternetGatewayDevice.LANDevice.1.WLANConfiguration.1.WPAEncryptionModes',
    name: 'encryptionType',
  });
  wifi24Arr1.push({
    key: 'InternetGatewayDevice.LANDevice.1.WLANConfiguration.1.PreSharedKey.1.KeyPassphrase',
    name: 'wpaSharedPassword',
  });
  wifi24Arr1.push({
    key: 'InternetGatewayDevice.LANDevice.1.WLANConfiguration.1.AutoChannelEnable',
    name: 'AutoChannelEnable',
  });
  const wifi58Arr = [];
  wifi58Arr.push({
    key: 'InternetGatewayDevice.LANDevice.1.WLANConfiguration.1.Enable',
    name: 'wifistate',
  });
  wifi58Arr.push({
    key: 'InternetGatewayDevice.LANDevice.1.WLANConfiguration.1.SSID',
    name: 'ssidName',
  });
  wifi58Arr.push({
    key: 'InternetGatewayDevice.LANDevice.1.WLANConfiguration.1.X_RZ_BandWidth',
    name: 'channelwide',
  });
  wifi58Arr.push({
    key: 'InternetGatewayDevice.LANDevice.1.WLANConfiguration.1.Channel',
    name: 'channel',
  });
  wifi58Arr.push({
    key: 'InternetGatewayDevice.LANDevice.1.WLANConfiguration.1.BeaconType',
    name: 'encryption',
  });
  wifi58Arr.push({
    key: 'InternetGatewayDevice.LANDevice.1.WLANConfiguration.1.WPAAuthenticationMode',
    name: 'WPAAuthenticationMode',
  });
  wifi58Arr.push({
    key: 'InternetGatewayDevice.LANDevice.1.WLANConfiguration.1.WPAEncryptionModes',
    name: 'encryptionType',
  });
  wifi58Arr.push({
    key: 'InternetGatewayDevice.LANDevice.1.WLANConfiguration.1.PreSharedKey.1.KeyPassphrase',
    name: 'wpaSharedPassword',
  });
  wifi58Arr.push({
    key: 'InternetGatewayDevice.LANDevice.1.WLANConfiguration.1.AutoChannelEnable',
    name: 'AutoChannelEnable',
  });
  const catvArr = [];
  catvArr.push({
    key: 'InternetGatewayDevice.X_RZ_Catv.RF_Enable',
    name: 'catvState',
  });
  catvArr.push({
    key: 'InternetGatewayDevice.X_RZ_Catv.PerformanceA',
    name: 'PerformanceA',
  });
  catvArr.push({
    key: 'InternetGatewayDevice.X_RZ_Catv.PerformanceB',
    name: 'PerformanceB',
  });
  const catvArr1 = [];
  // 0--关 1--开
  catvArr1.push({
    key: 'InternetGatewayDevice.X_RZ_RFDevice.CATVEnable',
    name: 'catvState',
  });
  // 取值1、2、3
  catvArr1.push({
    key: 'InternetGatewayDevice.X_RZ_RFDevice.AGCRange',
    name: 'AGCRange',
  });
  const deviceTypes = [ 'ZX8101L', 'ZX8212DWT', 'ZX8112WT', 'RX5102WT', 'RX5104WT', 'Modem/Router','ZX8414DWT','XKRC4' ];
  for (let j = 0; j < deviceTypes.length; j++) {
    const deviceType = deviceTypes[j];
    const obj = {
      id: ulid.ulid(),
      deviceType,
      typeName: 'WAN fix IP',
      keys: JSON.stringify(wanfixip),
      tid,
    };
    await me.ctx.modelmysql.Tr069Command.create(obj);
    const lanObj = {
      id: ulid.ulid(),
      deviceType,
      typeName: 'LAN',
      keys: JSON.stringify(lan),
      tid,
    };
    await me.ctx.modelmysql.Tr069Command.create(lanObj);
    const bridgeObj = {
      id: ulid.ulid(),
      deviceType,
      typeName: 'WAN Bridge',
      keys: JSON.stringify(bridgearr),
      tid,
    };
    await me.ctx.modelmysql.Tr069Command.create(bridgeObj);
    const sipObj = {
      id: ulid.ulid(),
      deviceType,
      typeName: 'SIP',
      keys: JSON.stringify(siparr),
      tid,
    };
    await me.ctx.modelmysql.Tr069Command.create(sipObj);
    const wanDHCPObj = {
      id: ulid.ulid(),
      deviceType,
      typeName: 'WAN DHCP',
      keys: JSON.stringify(wanDHCPArr),
      tid,
    };
    await me.ctx.modelmysql.Tr069Command.create(wanDHCPObj);
    const pppoeObj = {
      id: ulid.ulid(),
      deviceType,
      typeName: 'WAN PPPoE',
      keys: JSON.stringify(pppoeArr),
      tid,
    };
    await me.ctx.modelmysql.Tr069Command.create(pppoeObj);
    const wifi24Obj = {
      id: ulid.ulid(),
      deviceType,
      typeName: '2.4G wifi',
      keys: JSON.stringify(wifi24Arr),
      tid,
    };
    if (deviceType === 'ZX8112WT' || deviceType === 'RX5102WT' || deviceType === 'RX5104WT' || deviceType === 'Modem/Router') {
      wifi24Obj.keys = JSON.stringify(wifi24Arr1);
    }
    await me.ctx.modelmysql.Tr069Command.create(wifi24Obj);
    const wifi58Obj = {
      id: ulid.ulid(),
      deviceType,
      typeName: '5.8g wifi',
      keys: JSON.stringify(wifi58Arr),
      tid,
    };
    await me.ctx.modelmysql.Tr069Command.create(wifi58Obj);
    const catvObj = {
      id: ulid.ulid(),
      deviceType,
      typeName: 'catv',
      keys: JSON.stringify(catvArr),
      tid,
    };
    if (deviceType === 'RX5102WT' || deviceType === 'RX5104WT' || deviceType === 'Modem/Router'
    ) {
      catvObj.keys = JSON.stringify(catvArr1);
    }
    await me.ctx.modelmysql.Tr069Command.create(catvObj);
  }
  // const obj = {
  //   id: ulid.ulid(),
  //   deviceType: 'ZX8101L',
  //   typeName: 'WAN fix IP',
  //   keys: JSON.stringify(wanfixip),
  //   tid,
  // };
  // await me.ctx.model.Tr069Command.create(obj);
  // const lanObj = {
  //   id: ulid.ulid(),
  //   deviceType: 'ZX8101L',
  //   typeName: 'LAN',
  //   keys: JSON.stringify(lan),
  //   tid,
  // };
  // await me.ctx.model.Tr069Command.create(lanObj);
  // const bridgeObj = {
  //   id: ulid.ulid(),
  //   deviceType: 'ZX8101L',
  //   typeName: 'WAN Bridge',
  //   keys: JSON.stringify(bridgearr),
  //   tid,
  // };
  // await me.ctx.model.Tr069Command.create(bridgeObj);
  // const sipObj = {
  //   id: ulid.ulid(),
  //   deviceType: 'ZX8212DWT',
  //   typeName: 'SIP',
  //   keys: JSON.stringify(siparr),
  //   tid,
  // };
  // await me.ctx.model.Tr069Command.create(sipObj);
  // const wanDHCPObj = {
  //   id: ulid.ulid(),
  //   deviceType: 'ZX8101L',
  //   typeName: 'WAN DHCP',
  //   keys: JSON.stringify(wanDHCPArr),
  //   tid,
  // };
  // await me.ctx.model.Tr069Command.create(wanDHCPObj);
  // const pppoeObj = {
  //   id: ulid.ulid(),
  //   deviceType: 'ZX8101L',
  //   typeName: 'WAN PPPoE',
  //   keys: JSON.stringify(pppoeArr),
  //   tid,
  // };
  // await me.ctx.model.Tr069Command.create(pppoeObj);
  // const wifi24Obj = {
  //   id: ulid.ulid(),
  //   deviceType: 'ZX8212DWT',
  //   typeName: '2.4G wifi',
  //   keys: JSON.stringify(wifi24Arr),
  //   tid,
  // };
  // await me.ctx.model.Tr069Command.create(wifi24Obj);
  // const wifi58Obj = {
  //   id: ulid.ulid(),
  //   deviceType: 'ZX8212DWT',
  //   typeName: '5.8g wifi',
  //   keys: JSON.stringify(wifi58Arr),
  //   tid,
  // };
  // await me.ctx.model.Tr069Command.create(wifi58Obj);
  // const catvObj = {
  //   id: ulid.ulid(),
  //   deviceType: 'ZX8212DWT',
  //   typeName: 'catv',
  //   keys: JSON.stringify(catvArr),
  //   tid,
  // };
  // await me.ctx.model.Tr069Command.create(catvObj);

}


module.exports = TenantAdminService;
